package com.zheng.redpacket.server.controller.zookeeper;

import com.google.common.base.Strings;
import com.zheng.redpacket.api.CommonResult;
import com.zheng.redpacket.server.dto.BookRobDto;
import com.zheng.redpacket.server.serivce.BookRobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/book/rob")
public class BookRobController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BookRobController.class);

    @Autowired
    BookRobService bookRobService;

    @GetMapping("/request")
    public CommonResult takeMoney(BookRobDto dto) {
        if (dto.getUserId() == null || Strings.isNullOrEmpty(dto.getBookNo()) || dto.getUserId() <= 0) {
            return CommonResult.failed("参数校验失败");
        }
        try {
            //bookRobService.robWithNoLock(dto);
            //bookRobService.robWithLock(dto);
            bookRobService.robWithRedisson(dto);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("操作成功");
    }

}
